<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="${pageContext.request.contextPath }/common/plugin/zTree_v3-master/css/demo.css" type="text/css">
<link rel="stylesheet" href="${pageContext.request.contextPath }/common/plugin/zTree_v3-master/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${pageContext.request.contextPath }/common/plugin/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/plugin/zTree_v3-master/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/plugin/zTree_v3-master/js/jquery.ztree.excheck.js"></script> 
  <script type="text/javascript">
  var setting = {
		   /*  key: {
		         title:"permission_url"
		    }, */
		    
		    check: {
				enable: true
			},
		    
		data: {
				simpleData: {
					enable: true
				}
			},
			
		};
  
    /* function beforeClick(treeId, treeNode, clickFlag) {
		//判断是否为主菜单
		if(!treeNode.isParent){
			var url = treeNode.permission_url;
			var a = event.target.parentNode.parentNode;
			a.target="mainFrm";  //一个ifiame框架的名字
			a.href='${pageContext.request.contextPath}/+'url;
			
		}
	} */
	 
	var zTree;
	
	
	//定义初始化对应权限的函数
	function initNode(){
	
		zTree.expandAll(true);
		$.get("${pageContext.request.contextPath}/PermissionServlet?flag=getGrant&roleId=${param.roleId}",function(data){
	         var json = eval("("+data+")");
	       for(var i =0;i<json.length;i++){
	    	  zTree.checkNode(zTree.getNodeByParam("id",json[i].id),true);
	       }
		});
	}
	
	
	//初始化属性菜单
	$(document).ready(function() {
		
		$.get("${pageContext.request.contextPath}/PermissionServlet?flag=grant",function(data){
			//讲json转换成对象
		/* 	alert(eval("("+data+")")); */
	     //初始化树形菜单
		 $.fn.zTree.init($("#permissionTree"), setting, eval("("+data+")"));
		 zTree =$.fn.zTree.getZTreeObj("permissionTree");
		   initNode();
		});

		//定义授权按钮的点击事件
		$("#grant").bind("click",function(){
			//定义permissionId组成的字符串
			var permissionsId = "";
			var checkNodes =zTree.getCheckedNodes();
			for(var i=0;i<checkNodes.length;i++){
				var node = checkNodes[i];
				permissionsId+=node.id+",";
			}
			permissionsId = permissionsId.substr(0, permissionsId.length-1);
			var roleId = '${param.roleId}';
			location.href="PermissionServlet?flag=doGrant&permissionsId="+permissionsId+"&roleId="+roleId;
		});
		
	});
  </script>

</head>
<body>

    <div class="content_wrap">
	   <div class="left">
		   <ul id="permissionTree" class="ztree"></ul>
	   </div>
	<input type="button" id="grant" value="授权"/>
	</div>
	

</body>
</html>